* "When Do Mass Labor Strikes Reshape the Public" Replication Code
* Perspectives on Politics
* Alexander Hertel-Fernandez
* ah3467@columbia.edu

* Merge in distance to nearest stores

* Change directory and load data

* Set working directory with replication files here
* cd 

* Create blank dataset for mapping

use "sszipcoords.dta", clear
save "sszipcoords_stores.dta", replace

* Start log

log using "popstoredistance.log", replace

******** Stop and Shop Mapping

* Merge in distance to nearest Stop and Shop store

clear

forvalues s=1/253{
	use "StoreCoords/store`s'.dta", clear
	local tempstorelate = latstore`s'
	local tempstorelong = longstore`s'
	use "sszipcoords_stores.dta"
	gen tempstorelate = `tempstorelate'
	gen temstorelong = `tempstorelong'
	capture geodist intptlat intptlong tempstorelate temstorelong, generate(diststore`s')
	drop tempstorelate temstorelong
	save "sszipcoords_stores.dta", replace
}

* Next, find nearest Stop and Shop store and record value

reshape long diststore, i(v1) j(storeid)

sort v1 diststore

bys v1: gen temporder = _n 

by v1: gen neareststoredist = diststore if temporder==1
by v1: gen closeststoreid = storeid

keep if temporder==1

keep v1 neareststoredist closeststoreid

sum neareststoredist,d
label var neareststoredist "Nearest Stop & Shop Store, in Miles"

save "neareststoredist.dta", replace

******** Target Mapping

* Merge in distance to nearest Target store

clear

forvalues s=1/60{
	use "TargetCoords/target`s'.dta", clear
	local targettempstorelate = lattarget`s'
	local targettempstorelong = longtarget`s'
	use "sszipcoords_stores.dta"
	gen targettempstorelate = `targettempstorelate'
	gen targettemstorelong = `targettempstorelong'
	capture geodist intptlat intptlong targettempstorelate targettemstorelong, generate(disttarget`s')
	drop targettempstorelate targettemstorelong
	save "sszipcoords_stores.dta", replace
}

* Next, find nearest Target store and record value
 
egen targetneareststoredist = rmin(disttarget1-disttarget60)
sum targetneareststoredist,d
label var targetneareststoredist "Nearest Target Store, in Miles"

keep v1 targetneareststoredist

save "nearesttargetdist.dta", replace

******** Walmart Mapping

* Merge in distance to nearest Walmart store

clear

forvalues s=1/91{
	use "WalmartCoords/walmart`s'.dta", clear
	local waltempstorelate = latwal`s'
	local waltempstorelong = longwal`s'
	use "sszipcoords_stores.dta"
	gen waltempstorelate = `waltempstorelate'
	gen waltemstorelong = `waltempstorelong'
	capture geodist intptlat intptlong waltempstorelate waltemstorelong, generate(distwalmart`s')
	drop waltempstorelate waltemstorelong
	save "sszipcoords_stores.dta", replace
}

* Next, find nearest Walmart store and record value
 
egen walmartneareststoredist = rmin(distwalmart1-distwalmart91)
sum walmartneareststoredist,d
label var walmartneareststoredist "Nearest Walmart Store, in Miles"

keep v1 walmartneareststoredist

save "nearestwalmartdist.dta", replace

******** Market Basket Mapping

* Merge in distance to nearest Market Basket store

clear

forvalues s=1/46{
	use "MBCoords/market`s'.dta", clear
	local markettempstorelate = latmarket`s'
	local markettempstorelong = longmarket`s'
	use "sszipcoords_stores.dta"
	gen markettempstorelate = `markettempstorelate'
	gen markettemstorelong = `markettempstorelong'
	capture geodist intptlat intptlong markettempstorelate markettemstorelong, generate(distmarket`s')
	drop markettempstorelate markettemstorelong
	save "sszipcoords_stores.dta", replace
}

* Next, find nearest Market Basket store and record value
 
egen marketneareststoredist = rmin(distmarket1-distmarket46)
sum marketneareststoredist,d
label var marketneareststoredist "Nearest Market Basket Store, in Miles"

keep v1 marketneareststoredist

save "nearestmarketdist.dta", replace

******** Whole Foods Mapping

* Merge in distance to nearest Whole Foods store

clear

forvalues s=1/44{
	use "WFCoords/wf`s'.dta", clear
	local wftempstorelate = latwf`s'
	local wftempstorelong = longwf`s'
	use "sszipcoords_stores.dta"
	gen wftempstorelate = `wftempstorelate'
	gen wftemstorelong = `wftempstorelong'
	capture geodist intptlat intptlong wftempstorelate wftemstorelong, generate(distwf`s')
	drop wftempstorelate wftemstorelong
	save "sszipcoords_stores.dta", replace
}

* Next, find nearest Whole Foods store and record value
 
egen wfneareststoredist = rmin(distwf1-distwf44)
sum wfneareststoredist,d
label var wfneareststoredist "Nearest Whole Foods Store, in Miles"

keep v1 wfneareststoredist

save "nearestwfdist.dta", replace


******** Trader Joe's Mapping

* Merge in distance to nearest Trader Joe's store

clear

forvalues s=1/29{
	use "TJCoords/tj`s'.dta", clear
	local tjtempstorelate = lattj`s'
	local tjtempstorelong = longtj`s'
	use "sszipcoords_stores.dta"
	gen tjtempstorelate = `tjtempstorelate'
	gen tjtemstorelong = `tjtempstorelong'
	capture geodist intptlat intptlong tjtempstorelate tjtemstorelong, generate(disttj`s')
	drop tjtempstorelate tjtemstorelong
	save "sszipcoords_stores.dta", replace
}

* Next, find nearest Trader Joe's store and record value
 
egen tjneareststoredist = rmin(disttj1-disttj29)
sum tjneareststoredist,d
label var tjneareststoredist "Nearest Trader Joe's Store, in Miles"

keep v1 tjneareststoredist

save "nearesttjdist.dta", replace

**** Final merges

use "sszipcoords.dta", clear

merge 1:1 v1 using "nearesttargetdist.dta"
drop if _m==2
drop _m

merge 1:1 v1 using "neareststoredist.dta"
drop if _m==2
drop _m

merge 1:1 v1 using "nearestwalmartdist.dta"
drop if _m==2
drop _m

merge 1:1 v1 using "nearestmarketdist.dta"
drop if _m==2
drop _m

merge 1:1 v1 using "nearestwfdist.dta"
drop if _m==2
drop _m

merge 1:1 v1 using "nearesttjdist.dta"
drop if _m==2
drop _m

egen walmarttargetnearestdist = rmin(walmartneareststoredist targetneareststoredist)
label var walmarttargetnearestdist "Nearest Walmart or Target Store, in Miles"

egen grocnearestdist = rmin(tjneareststoredist marketneareststoredist  wfneareststoredist)
label var grocnearestdist "Nearest Other Grocery Store, in Miles"

egen anystorenearestdist = rmin( targetneareststoredist walmartneareststoredist tjneareststoredist marketneareststoredist  wfneareststoredist)
label var anystorenearestdist "Nearest Other Store, in Miles"

* Close log

log close

